﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Nxg.Blog.Core.ViewProjections.Home {
    public class TagCloudBindingModel {
        public int Threshold { get; set; } = 1;

        public int Take { get; set; } = int.MaxValue;
    }

    public class TagCloudViewModel {
        public IEnumerable<Documents.Tag> Tags { get; set; }
    }

    public class TagCloudViewProjection : IViewProjection<TagCloudBindingModel, TagCloudViewModel> {
        private readonly DataContext _dataContext;

        public TagCloudViewProjection (DataContext context) {
            _dataContext = context;
        }

        public TagCloudViewModel Project (TagCloudBindingModel input) {
            var tags = _dataContext.Tag
                .OrderByDescending (x => x.PostCount)
                .Skip (input.Threshold - 1)
                .Take (input.Take).ToList ();
            return new TagCloudViewModel {
                Tags = tags
            };

        }
    }
}